const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter
const status_list = getApp().globalData.status_list.slice(1,)

// pages/export_flight_logs/export_flight_logs.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status_list,
    label_id:'',
    status_id:'',
    label_list:[],
  },


  select_label(e){
    getApp().get_vibration()
    this.setData({
      label_id:e.currentTarget.id
    })
    this.load_stu_count(this.data.jg_id,this.data.label_id, this.data.status_id)
  },

  select_status(e){
    getApp().get_vibration()
    this.setData({
      status_id:e.currentTarget.id
    })
    this.load_stu_count(this.data.jg_id,this.data.label_id, this.data.status_id)
  },

  
  load_stu_count(jg_id, label_id, status_id){
    wx.request({
      url: domainName + 'export_stu_info_stu_count',
      data: {jg_id, label_id, status_id}, ...parameter,
      success: res => {
        this.setData({
          stu_count:res.data.stu_count
        })
      },
    })
  },


  // 确认导出
  confirm(){
    getApp().get_vibration()
    if(!this.data.label_id){
      wx.showModal({
        content: '请筛选分组/批次',
        showCancel: false,
        title: '提示',
      })
    }else if(!this.data.status_id){
      wx.showModal({
        content: '请筛选学员状态',
        showCancel: false,
        title: '提示',
      })
    }else if(this.data.stu_count>0){
      wx.showModal({
        content: '确定导出数据吗？',
        showCancel: true,
        title: '提示',
        success: res => {
          if(res.confirm){
            wx.showLoading({
              title: '打包数据中……',
            })
            wx.request({
              url: domainName + 'export_stu_info_export',
              data: {
                jg_id:this.data.jg_id,
                status_id:this.data.status_id,
                label_id:this.data.label_id,
              },...parameter,
              success: res => {
                wx.hideLoading()
                // console.log('res', res.data)
                let zip_name = res.data.zip_name
                if(zip_name){
                    // callback 写法
                    wx.showLoading({
                      title: '正常下载文件（数据较大，等待较久）……',
                    })
                    wx.downloadFile({
                      url: domainName + zip_name,
                      success: res => {
                        wx.hideLoading()
                        console.log('res',res)
                        if (res.statusCode === 200) {
                          // 下载完成后转发
                          wx.shareFileMessage({
                            filePath: res.tempFilePath,
                          })

                          // 删除服务器文件
                          wx.request({
                            url: domainName + '/export_stu_info_rmFile',
                            data: {
                              zip_name,
                            },...parameter,
                            success: res => {
                              console.log('删除服务器文件',res)
                            },
                          })
                        }else{
                          wx.showToast({
                            title: '文件读取失败',
                            duration: 1500,
                            icon: 'error',
                            mask: false,
                          })
                        }
                      },
                      fail:err=>{
                        wx.showToast({
                          title: '下载出错',
                          icon: 'error',
                        })
                      }
                    })
                }else{
                  wx.showToast({
                    title: '导出出错误',
                    icon: 'error',
                    mask: false,
                  })
                }
              },
            })
          }
        },
      })
  
    }else{
      wx.showToast({
        title: '人数为 0',
        icon: 'error',
      })
    }

  },

  page_init(jg_id){
    wx.request({
      url: domainName + 'export_stu_info_init',
      data: {jg_id},...parameter,
      success: res => {
        this.setData({
          label_list:res.data.label_list,
        })
        
      },
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.jg_id = options.jg_id
    this.page_init(this.data.jg_id)
  },

})